import json

from django.core import serializers
from rest_framework.response import Response
from rest_framework.views import APIView

from hospital_model.models import AgreementModel, DepartmentModel, ProjectModel, TimeModel, DataTimeModel, \
    TimeQuantumModel
from hospital_model.serializer import DepartmentViewSerializer
from user_model.models import ComplainModel
from datetime import datetime


# 查询投诉建议
class SuggestionView(APIView):
    def get(self, *args, **kwargs):
        data = []
        time = str(datetime.now().strftime('%Y-%m-%d'))
        print(type(time), time)
        all_complain = ComplainModel.objects.filter(c_time__contains=time)
        data_lists = (json.loads(serializers.serialize('json', all_complain)))
        for data_list in data_lists:
            data.append(data_list['fields'])
        data = {
            'msg': "查询当天投诉建议",
            'code': 2000,
            'data': data
        }
        return Response(data)


# 协议表
class AgreementView(APIView):
    def get(self, *args, **kwargs):
        agreement = AgreementModel.objects.filter(a_status='0').all()
        agreements = (json.loads(serializers.serialize('json', agreement)))[0]
        data = agreements['fields']
        data = {
            'msg': "查询当天投诉建议",
            'code': 2000,
            'data': data
        }
        return Response(data)

    def post(self, request, *args, **kwargs):
        content = request.POST['content']
        AgreementModel.objects.create(a_content=content)
        data = {
            'msg': "添加成功",
            'code': 2000,
        }
        return Response(data)


# 获取科室及其预约项目
class DepartmentView(APIView):
    def get(self, *args, **kwargs):
        list_data = []
        department = DepartmentModel.objects.all()
        agreements = (json.loads(serializers.serialize('json', department)))
        for agreement in agreements:
            dict_data = {}
            dict_data['project_name'] = agreement['fields']['d_name']
            agreement_json = ProjectModel.objects.filter(p_key=agreement['pk']).all()
            if agreement_json.count() == 0:
                dict_data['project'] = []
                list_data.append(dict_data)
            else:
                project_list = []
                agreement_list = (json.loads(serializers.serialize('json', agreement_json)))
                for agreement_li in agreement_list:
                    json_data = {}
                    project_id = agreement_li['pk']
                    project_name = agreement_li['fields']['p_name']
                    p_money = agreement_li['fields']['p_money']
                    p_prepay = agreement_li['fields']['p_prepay']
                    p_note = agreement_li['fields']['p_note']
                    p_number = agreement_li['fields']['p_number']
                    json_data['project_id'] = project_id
                    json_data['project_name'] = project_name
                    json_data['p_money'] = p_money
                    json_data['p_prepay'] = p_prepay
                    json_data['p_note'] = p_note
                    json_data['p_number'] = p_number
                    # print(json_data)
                    project_list.append(json_data)
                dict_data['project'] = project_list
                list_data.append(dict_data)

        data = {
            'msg': "查询成功",
            'code': 2000,
            'data': list_data
        }
        return Response(data)


# 体检预约查询
class OldMedicalView(APIView):
    def post(self, request, *args, **kwargs):
        # 体检项目id
        tj_id = request.POST['t_id']
        agreement_json = TimeQuantumModel.objects.filter(project__p_id=tj_id).all()
        agreement_lists = (json.loads(serializers.serialize('json', agreement_json)))
        p_json = ProjectModel.objects.filter(p_id=tj_id)
        p = (json.loads(serializers.serialize('json', p_json)))[0]
        time_quantum_list = []
        for agreement_list in agreement_lists:
            ProjectModel.objects.filter()
            time_quantum_dict = {}
            point_in_time = []
            time_quantum_dict['pk'] = tj_id
            time_quantum_dict['p_name'] = p['fields']['p_name']
            time_quantum_dict['p_money'] = p['fields']['p_money']
            time_quantum_dict['p_prepay'] = p['fields']['p_prepay']
            time_quantum_dict['p_note'] = p['fields']['p_note']
            time_quantum_dict['p_day'] = p['fields']['p_day']
            time_quantum_dict['p_number'] = p['fields']['p_number']
            time_quantum_dict['t_name'] = agreement_list['fields']['t_name']
            time_quantum_dict['start_time'] = agreement_list['fields']['start_time']
            time_quantum_dict['end_time'] = agreement_list['fields']['end_time']
            data_jsons = DataTimeModel.objects.filter(time__t_id=agreement_list['pk']).all()
            serialization_list = (json.loads(serializers.serialize('json', data_jsons)))
            week_list = []
            for i in serialization_list:
                week_list.append(i['fields']['d_data'])
            time_quantum_dict['week'] = week_list
            for a in agreement_list['fields']['d_time']:
                data_jsons = TimeModel.objects.filter(t_id=a)
                point_in_time_dict = {}
                serialization_li = (json.loads(serializers.serialize('json', data_jsons)))
                for b in serialization_li:
                    # 时间点
                    point_in_time_dict['time_id'] = b['pk']
                    point_in_time_dict['t_name'] = b['fields']['t_name']
                    point_in_time_dict['t_number'] = b['fields']['t_number']
                    point_in_time_dict['t_start_time'] = b['fields']['t_start_time']
                    point_in_time_dict['t_end_time'] = b['fields']['t_end_time']
                point_in_time.append(point_in_time_dict)
            time_quantum_dict['datetime'] = point_in_time
            time_quantum_list.append(time_quantum_dict)
        data = {
            'msg': "查询成功",
            'code': 2000,
            'data': time_quantum_list
        }
        return Response(data)

"""
# 体检预约查询
class OldMedicalView(APIView):
    def post(self, request, *args, **kwargs):
        # 体检项目id
        tj_id = request.POST['t_id']
        agreement_json = TimeQuantumModel.objects.filter(project__p_id=tj_id).all()
        agreement_lists = (json.loads(serializers.serialize('json', agreement_json)))
        # print(type(ProjectModel.objects.filter(p_id=tj_id).first()))
        p_json = ProjectModel.objects.filter(p_id=tj_id)
        p = (json.loads(serializers.serialize('json', p_json)))[0]
        time_quantum_list = []
        for agreement_list in agreement_lists:
            ProjectModel.objects.filter()
            time_quantum_dict = {}
            point_in_time = []
            time_quantum_dict['pk'] = agreement_list['pk']
            time_quantum_dict['p_name'] = p['fields']['p_name']
            time_quantum_dict['p_money'] = p['fields']['p_money']
            time_quantum_dict['p_prepay'] = p['fields']['p_prepay']
            time_quantum_dict['p_note'] = p['fields']['p_note']
            time_quantum_dict['p_day'] = p['fields']['p_day']
            time_quantum_dict['p_number'] = p['fields']['p_number']
            time_quantum_dict['t_name'] = agreement_list['fields']['t_name']
            time_quantum_dict['start_time'] = agreement_list['fields']['start_time']
            time_quantum_dict['end_time'] = agreement_list['fields']['end_time']
            data_jsons = DataTimeModel.objects.filter(time__t_id=agreement_list['pk']).all()
            serialization_list = (json.loads(serializers.serialize('json', data_jsons)))
            week_list = []
            for i in serialization_list:
                week_list.append(i['fields']['d_data'])
            time_quantum_dict['week'] = week_list
            for a in agreement_list['fields']['d_time']:
                data_jsons = TimeModel.objects.filter(t_id=a)
                point_in_time_dict = {}
                serialization_li = (json.loads(serializers.serialize('json', data_jsons)))
                for b in serialization_li:
                    # 时间点
                    point_in_time_dict['t_name'] = b['fields']['t_name']
                    point_in_time_dict['t_number'] = b['fields']['t_number']
                    point_in_time_dict['t_start_time'] = b['fields']['t_start_time']
                    point_in_time_dict['t_end_time'] = b['fields']['t_end_time']
                point_in_time.append(point_in_time_dict)
            time_quantum_dict['datetime'] = point_in_time
            time_quantum_list.append(time_quantum_dict)
        data = {
            'msg': "查询成功",
            'code': 2000,
            'data': time_quantum_list
        }
        return Response(data)
"""
